home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / c_spec / execute / du.c < prev    next >
C/C++ Source or Header  |  1986-02-20  |  2KB  |  94 lines

  1. #include <stdio.h>
  2. #include "/include/mydos.h"
  3.  
  4. /*    DU:        Get Disk Usage. Returns the number of used
  5.  *            and avaliable bytes on the indicated
  6.  *    drive. Usage is: du [<drive> ... <drive>] where <drive> is
  7.  *    one of a, b, etc.
  8.  *
  9.  *    Copyright (C) 1986, Allen I. Holub. All rights reserved.
  10.  */
  11.  
  12. #define E(x)    fprintf(stderr, "%s\n", x )
  13.  
  14.  
  15. main( argc, argv )
  16. char    **argv;
  17. {
  18.     REGS        regs;
  19.     register int    verbose = 0 ;
  20.  
  21.     ctlc();
  22.  
  23.     if( argc > 1  &&  argv[1][0] == '-' )
  24.     {
  25.         if( !( verbose = (argv[1][1] == 'v')) )
  26.             usage();
  27.         argv++;
  28.         --argc;
  29.     }
  30.  
  31.     if( argc == 1  )
  32.     {
  33.         gregs( ®s );
  34.         regs.h.ah = 0x19 ;    /* Get current disk id    */
  35.         mydos( ®s );
  36.  
  37.         printf("%c: ", regs.h.al + 'A' );
  38.         dodrive( 0, verbose );
  39.     }
  40.     else
  41.     {
  42.         for( ++argv ; --argc > 0 ; ++argv )
  43.         {
  44.             printf("%c: ", toupper( **argv ) );
  45.             dodrive( (toupper(**argv) - 'A') + 1, verbose );
  46.         }
  47.     }
  48. }
  49.  
  50. /*----------------------------------------------------------------------*/
  51.  
  52. usage()
  53. {
  54.     E("du:   Copyright (c) 1986, Allen I. Holub, All rights reserved.");
  55.     E("\nUsage: du [-v] [disk id]\n" );
  56.     E("Prints the disk capacity, amount of space used, and amount");
  57.     E("of space available. The optional disk id can be a: b: ,etc., or ");
  58.     E("a b c, etc. If no drive is specified, the current drive is used");
  59.     E("The percentage of space available is also given.");
  60.     E("The -v flag gives a more verbose output.\n");
  61.     exit(1);
  62. }
  63.  
  64. /*----------------------------------------------------------------------*/
  65.  
  66. dodrive( id, verbose )
  67. {
  68.     unsigned spc, bps, ac, tc;
  69.     double     total, avail ;
  70.  
  71.     if( !diskinfo( id, &spc, &bps, &ac, &tc ) )
  72.         printf("Can't access drive\n");
  73.     else
  74.     {
  75.         total = (double)( (double)spc * (double)bps * (double)tc );
  76.         avail = (double)( (double)spc * (double)bps * (double)ac );
  77.  
  78.         printf("%7.1f K bytes total, ",  total/1024.0 );
  79.  
  80.         printf("%7.1f K used, ", (total-avail)/1024.0 );
  81.  
  82.         printf("%7.1f K (%05.2f%% of disk) available\n", 
  83.                 avail/1024.0, 100.0 * (avail/total) );
  84.         if( verbose )
  85.         {
  86.             printf("    Sectors per Cluster . %-9d",     spc );
  87.             printf("    Bytes per Sector .... %-9d\n",   bps );
  88.             printf("    Available Clusters .. %-9d",     ac  );
  89.             printf("    Total Clusters ...... %-9d\n\n", tc  );
  90.         }
  91.     }
  92. }
  93.  
  94.